package com.bzkj.basicinfo.utils;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.bzkj.basicinfo.domain.ncc.*;
import com.bzkj.basicinfo.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;

@Component
public class NccBasicUtil {

    @Autowired
    private NccBasicSkuMapper nccBasicSkuMapper;
    @Autowired
    private NccBasicUserInfoMapper nccBasicUserInfoMapper;
    @Autowired
    private NccBasicCustomerMapper nccBasicCustomerMapper;
    @Autowired
    private NccBasicSupplierMapper nccBasicSupplierMapper;
    @Autowired
    private NccBasicStoStockMapper nccBasicStoStockMapper;
    @Autowired
    private NccBasicManufacturerInfoMapper nccBasicManufacturerInfoMapper;
    @Autowired
    private NccBasicBillsTypeMapper nccBasicBillsTypeMapper;
    @Autowired
    private NccMaterialManufacturerMapper nccMaterialManufacturerMapper;
    @Autowired
    private NccBillStatusMapper nccBillStatusMapper;
    @Autowired
    private NccBasicDeptMapper nccBasicDeptMapper;

    @Value("${ncc.baseUrl}")
    private static String url;

    @Value("${ncc.basicInfoUrl}")
    private static String basicInfoUrl;

    @Value("${ncc.sourceSystem}")
    private static String sourceSystem;

    @Value("${ncc.targetSystem}")
    private static String targetSystem;

    @Value("${ncc.serviceName}")
    private static String serviceName;

    @Value("${ncc.contentType}")
    private static String contentType;

    public void ncc(Integer i, Integer platformCode) {
        // 定义时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 获取昨天的日期
        LocalDate yesterday = LocalDate.now().minusDays(0);
        // 昨天的23:59:59
        LocalDateTime endOfYesterday = yesterday.atTime(23, 59, 59);
        String endOfYesterdayStr = endOfYesterday.format(formatter);
        String startOfYesterdayStr = "2022-01-01 00:00:00";
        // 动态调用方法
        String methodName = "processDateRange_" + i;
        try {
            Method method = this.getClass().getMethod(methodName, String.class, String.class, Integer.class, Integer.class);
            method.invoke(this, startOfYesterdayStr, endOfYesterdayStr, i, platformCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void processDateRange_1(String startTimeStr, String endTimeStr, Integer i, Integer platformCode) {
        HttpResponse response = methodExecute(startTimeStr, endTimeStr, i, platformCode);

        if (response.isOk()) {
            String responseBody = response.body();
            JSONObject entries = JSONUtil.parseObj(responseBody);
            // 获取 data 数组
            JSONArray dataArray = entries.getJSONArray("data");
            // 将 JSONArray 转换为 List<NccBasicSkuInfoData>
            List<NccBasicSkuInfoData> skuInfoDataList = dataArray.toList(NccBasicSkuInfoData.class);

            if (!skuInfoDataList.isEmpty()) {
                for (NccBasicSkuInfoData skuInfo : skuInfoDataList) {
                    NccBasicSkuInfo nccBasicSkuInfo = new NccBasicSkuInfo();
                    nccBasicSkuInfo.setPk_material(skuInfo.getpK_MATERIAL());
                    nccBasicSkuInfo.setCode(skuInfo.getCode());
                    nccBasicSkuInfo.setName(skuInfo.getName());
                    nccBasicSkuInfo.setMaterialshortname(skuInfo.getMaterialshortname());
                    nccBasicSkuInfo.setMaterialspec(skuInfo.getMaterialspec());
                    nccBasicSkuInfo.setMaterialmnecode(skuInfo.getMaterialmnecode());
                    nccBasicSkuInfo.setMaterialtype(skuInfo.getMaterialtype());
                    nccBasicSkuInfo.setPk_marbasclass(skuInfo.getpK_MARBASCLASS());
                    nccBasicSkuInfo.setPk_measdoc(skuInfo.getpK_MEASDOC());
                    nccBasicSkuInfo.setProdarea(skuInfo.getProdarea());
                    nccBasicSkuInfo.setManufactory(skuInfo.getManufactory());
                    nccBasicSkuInfo.setPk_org(skuInfo.getpK_ORG());
                    nccBasicSkuInfo.setPk_group(skuInfo.getpK_GROUP());
                    nccBasicSkuInfo.setPk_source(skuInfo.getpK_SOURCE());
                    nccBasicSkuInfo.setEnable_state(skuInfo.getEnablestate());
                    nccBasicSkuInfo.setCreator(skuInfo.getCreator());
                    nccBasicSkuInfo.setCreateTime(skuInfo.getCreationtime());
                    nccBasicSkuInfo.setModifier(skuInfo.getModifier());
                    nccBasicSkuInfo.setModifierTime(skuInfo.getModifiedtime());
                    nccBasicSkuInfo.setTsTime(skuInfo.getTs());
                    nccBasicSkuInfo.setDef1(skuInfo.getDeF1());
                    nccBasicSkuInfo.setDef2(skuInfo.getDeF2());
                    nccBasicSkuInfo.setDef3(skuInfo.getDeF3());
                    nccBasicSkuInfo.setDef4(skuInfo.getDeF4());
                    nccBasicSkuInfo.setDef5(skuInfo.getDeF5());

                    // 根据 code 查询数据库
                    NccBasicSkuInfo queryNccBasicSkuInfo = nccBasicSkuMapper.queryNccBasicSkuInfo(skuInfo.getCode());
                    if (queryNccBasicSkuInfo != null) {
                        // 如果存在，更新数据
                        nccBasicSkuMapper.updateNccBasicSkuInfo(nccBasicSkuInfo);
                    } else {
                        // 如果不存在，新增数据
                        nccBasicSkuMapper.insertNccBasicSkuInfo(nccBasicSkuInfo);
                    }
                }
            }
        }
    }

    public void processDateRange_2(String startTimeStr, String endTimeStr, Integer i, Integer platformCode) {

        HttpResponse response = methodExecute(startTimeStr, endTimeStr, i, platformCode);

        if (response.isOk()) {
            String responseBody = response.body();
            JSONObject entries = JSONUtil.parseObj(responseBody);
            JSONArray dataArray = entries.getJSONArray("data");
            List<NccBasicSkuTypeData> skuTypeDataList = dataArray.toList(NccBasicSkuTypeData.class);
            for (NccBasicSkuTypeData nccBasicSkuTypeData : skuTypeDataList) {
                NccBasicSkuType nccBasicSkuType = new NccBasicSkuType();
                nccBasicSkuType.setNccStuCode(nccBasicSkuTypeData.getCode());
                nccBasicSkuType.setNccStuName(nccBasicSkuTypeData.getName());
                nccBasicSkuType.setPkMarbasclass(nccBasicSkuTypeData.getpK_MARBASCLASS());
                nccBasicSkuType.setCreatetime(nccBasicSkuTypeData.getCreateTime());
                nccBasicSkuType.setPkOrg(nccBasicSkuType.getPkOrg());
                nccBasicSkuType.setPkGroup(nccBasicSkuType.getPkGroup());
                nccBasicSkuType.setEnableState(nccBasicSkuType.getEnableState());
                nccBasicSkuType.setTstime(nccBasicSkuTypeData.getTs());
                nccBasicSkuType.setCreator(nccBasicSkuTypeData.getCreator());
                nccBasicSkuType.setDef1(nccBasicSkuTypeData.getDeF1());
                nccBasicSkuType.setDef2(nccBasicSkuTypeData.getDeF2());
                nccBasicSkuType.setDef3(nccBasicSkuTypeData.getDeF3());
                nccBasicSkuType.setDef4(nccBasicSkuTypeData.getDeF4());
                nccBasicSkuType.setDef5(nccBasicSkuTypeData.getDeF5());

                NccBasicSkuType nccBasicSkuType1 = nccBasicSkuMapper.queryNccBasicSkuType(nccBasicSkuTypeData.getCode());
                if (nccBasicSkuType1 != null) {
                    // 如果存在，更新数据
                    nccBasicSkuMapper.updateNccBasicSkuType(nccBasicSkuType);
                } else {
                    // 如果不存在，新增数据
                    nccBasicSkuMapper.insertNccBasicSkuType(nccBasicSkuType);
                }
            }

        }
    }

    public void processDateRange_3(String startTimeStr, String endTimeStr, Integer i, Integer platformCode) {

        HttpResponse response = methodExecute(startTimeStr, endTimeStr, i, platformCode);
        if (response.isOk()) {
            String responseBody = response.body();
            JSONObject entries = JSONUtil.parseObj(responseBody);
            JSONArray dataArray = entries.getJSONArray("data");
            List<NccBasicDepartment> nccBasicDepartments = dataArray.toList(NccBasicDepartment.class);
            for (NccBasicDepartment basicDepartment : nccBasicDepartments) {
                NccBasicDepartment basicDepartment1 = nccBasicSkuMapper.queryNccBasicDepartment(basicDepartment.getCode());
                if (basicDepartment1 != null) {
                    nccBasicSkuMapper.updateNccBasicDepartment(basicDepartment);
                } else {
                    nccBasicSkuMapper.insertNccBasicDepartment(basicDepartment);
                }
            }
        }
    }

    public void processDateRange_4(String startTimeStr, String endTimeStr, Integer i, Integer platformCode) {

        HttpResponse response = methodExecute(startTimeStr, endTimeStr, i, platformCode);
        if (response.isOk()) {
            String responseBody = response.body();
            JSONObject entries = JSONUtil.parseObj(responseBody);
            JSONArray dataArray = entries.getJSONArray("data");
            List<NccBasicReducedUnit> nccBasicReducedUnits = dataArray.toList(NccBasicReducedUnit.class);
            for (NccBasicReducedUnit reducedUnit : nccBasicReducedUnits) {
                NccBasicReducedUnit reducedUnit1 = nccBasicSkuMapper.queryNccBasicReducedUnit(reducedUnit.getpK_MATERIAL());
                if (reducedUnit1 != null) {
                    nccBasicSkuMapper.updateNccBasicReducedUnit(reducedUnit);
                } else {
                    nccBasicSkuMapper.insertNccBasicReducedUnit(reducedUnit);
                }
            }
        }
    }

    public void processDateRange_5(String startTimeStr, String endTimeStr, Integer i, Integer platformCode) {

        HttpResponse response = methodExecute(startTimeStr, endTimeStr, i, platformCode);
        if (response.isOk()) {
            String responseBody = response.body();
            JSONObject entries = JSONUtil.parseObj(responseBody);
            JSONArray dataArray = entries.getJSONArray("data");
            List<NccBasicUserInfo> basicUserInfos = dataArray.toList(NccBasicUserInfo.class);
            for (NccBasicUserInfo basicUserInfo : basicUserInfos) {
                NccBasicUserInfo basicCustomer1 = nccBasicUserInfoMapper.queryNccBasicUserInfo(basicUserInfo.getCode());
                if (basicCustomer1 != null) {
                    nccBasicUserInfoMapper.updateNccBasicUserInfo(basicUserInfo);
                } else {
                    nccBasicUserInfoMapper.insertNccBasicUserInfo(basicUserInfo);
                }
            }
        }
    }

    public void processDateRange_6(String startTimeStr, String endTimeStr, Integer i, Integer platformCode) {

        HttpResponse response = methodExecute(startTimeStr, endTimeStr, i, platformCode);
        if (response.isOk()) {
            String responseBody = response.body();
            JSONObject entries = JSONUtil.parseObj(responseBody);
            JSONArray dataArray = entries.getJSONArray("data");
            List<NccBasicCustomer> basicCustomers = dataArray.toList(NccBasicCustomer.class);
            for (NccBasicCustomer basicCustomer : basicCustomers) {
                NccBasicCustomer basicCustomer1 = nccBasicCustomerMapper.queryNccBasicCustomer(basicCustomer.getCode());
                if (basicCustomer1 != null) {
                    nccBasicCustomerMapper.updateNccBasicCustomer(basicCustomer);
                } else {
                    nccBasicCustomerMapper.insertNccBasicCustomer(basicCustomer);
                }
            }
        }
    }

    public void processDateRange_7(String startTimeStr, String endTimeStr, Integer i, Integer platformCode) {

        HttpResponse response = methodExecute(startTimeStr, endTimeStr, i, platformCode);
        if (response.isOk()) {
            String responseBody = response.body();
            JSONObject entries = JSONUtil.parseObj(responseBody);
            JSONArray dataArray = entries.getJSONArray("data");
            List<NccBasicSupplier> basicSuppliers = dataArray.toList(NccBasicSupplier.class);
            for (NccBasicSupplier basicCustomer : basicSuppliers) {
                NccBasicSupplier basicCustomer1 = nccBasicSupplierMapper.queryNccBasicSupplier(basicCustomer.getCode());
                if (basicCustomer1 != null) {
                    nccBasicSupplierMapper.updateNccBasicSupplier(basicCustomer);
                } else {
                    nccBasicSupplierMapper.insertNccBasicSupplier(basicCustomer);
                }
            }
        }
    }

    public void processDateRange_8(String startTimeStr, String endTimeStr, Integer i, Integer platformCode) {

        HttpResponse response = methodExecute(startTimeStr, endTimeStr, i, platformCode);
        if (response.isOk()) {

            String responseBody = response.body();
            JSONObject entries = JSONUtil.parseObj(responseBody);
            JSONArray dataArray = entries.getJSONArray("data");
            List<NccBasicStoStock> basicStoStocks = dataArray.toList(NccBasicStoStock.class);
            for (NccBasicStoStock basicStoStock : basicStoStocks) {
                NccBasicStoStock basicStoStock1 = nccBasicStoStockMapper.queryNccBasicStoStock(basicStoStock.getCode());
                if (basicStoStock1 != null) {
                    nccBasicStoStockMapper.updateNccBasicStoStock(basicStoStock);
                } else {
                    nccBasicStoStockMapper.insertNccBasicStoStock(basicStoStock);
                }
            }
        }
    }

    public void processDateRange_9(String startTimeStr, String endTimeStr, Integer i, Integer platformCode) {

        HttpResponse response = methodExecute(startTimeStr, endTimeStr, i, platformCode);
        if (response.isOk()) {

            String responseBody = response.body();
            JSONObject entries = JSONUtil.parseObj(responseBody);
            JSONArray dataArray = entries.getJSONArray("data");
            List<NccBasicManufacturerInfo> basicManufacturerInfos = dataArray.toList(NccBasicManufacturerInfo.class);
            for (NccBasicManufacturerInfo basicManufacturerInfo : basicManufacturerInfos) {
                NccBasicManufacturerInfo basicManufacturerInfo1 = nccBasicManufacturerInfoMapper.queryNccBasicManufacturerInfo(basicManufacturerInfo.getCode());
                if (basicManufacturerInfo1 != null) {
                    nccBasicManufacturerInfoMapper.updateNccBasicManufacturerInfo(basicManufacturerInfo);
                } else {
                    nccBasicManufacturerInfoMapper.insertNccBasicManufacturerInfo(basicManufacturerInfo);
                }
            }
        }
    }

    public void processDateRange_10(String startTimeStr, String endTimeStr, Integer i, Integer platformCode) {

        HttpResponse response = methodExecute(startTimeStr, endTimeStr, i, platformCode);
        if (response.isOk()) {

            String responseBody = response.body();
            JSONObject entries = JSONUtil.parseObj(responseBody);
            JSONArray dataArray = entries.getJSONArray("data");
            List<NccBasicBillsType> basicBillsTypes = dataArray.toList(NccBasicBillsType.class);
            for (NccBasicBillsType basicBillsType : basicBillsTypes) {
                NccBasicBillsType basicCustomer1 = nccBasicBillsTypeMapper.queryNccBasicBillsType(basicBillsType.getNodeCode());
                if (basicCustomer1 != null) {
                    nccBasicBillsTypeMapper.updateNccBasicBillsType(basicBillsType);
                } else {
                    nccBasicBillsTypeMapper.insertNccBasicBillsType(basicBillsType);
                }
            }
        }
    }

    public void processDateRange_11(String startTimeStr, String endTimeStr, Integer i, Integer platformCode) {

        HttpResponse response = methodExecute(startTimeStr, endTimeStr, i, platformCode);
        if (response.isOk()) {
            String responseBody = response.body();
            JSONObject entries = JSONUtil.parseObj(responseBody);
            JSONArray dataArray = entries.getJSONArray("data");
            List<NccMaterialManufacturer> materialManufacturers = dataArray.toList(NccMaterialManufacturer.class);
            for (NccMaterialManufacturer materialManufacturer : materialManufacturers) {
                NccMaterialManufacturer basicCustomer1 = nccMaterialManufacturerMapper.queryNccMaterialManufacturer(materialManufacturer.getCode());
                if (basicCustomer1 != null) {
                    nccMaterialManufacturerMapper.updateNccMaterialManufacturer(materialManufacturer);
                } else {
                    nccMaterialManufacturerMapper.insertNccMaterialManufacturer(materialManufacturer);
                }
            }
        }
    }

    public void processDateRange_12(String startTimeStr, String endTimeStr, Integer i, Integer platformCode) {

        HttpResponse response = methodExecute(startTimeStr, endTimeStr, i, platformCode);
        if (response.isOk()) {
            String responseBody = response.body();
            JSONObject entries = JSONUtil.parseObj(responseBody);
            JSONArray dataArray = entries.getJSONArray("data");
            List<NccBillStatus> nccBillStatuses = dataArray.toList(NccBillStatus.class);
            for (NccBillStatus nccBillStatus : nccBillStatuses) {
                NccBillStatus basicCustomer1 = nccBillStatusMapper.queryNccNccBillStatus(nccBillStatus.getpK_STORESTATE());
                if (basicCustomer1 != null) {
                    nccBillStatusMapper.updateNccNccBillStatus(nccBillStatus);
                } else {
                    nccBillStatusMapper.insertNccNccBillStatus(nccBillStatus);
                }
            }
        }
    }

    public void processDateRange_13(String startTimeStr, String endTimeStr, Integer i, Integer platformCode) {

        HttpResponse response = methodExecute(startTimeStr, endTimeStr, i, platformCode);
        if (response.isOk()) {

            String responseBody = response.body();
            JSONObject entries = JSONUtil.parseObj(responseBody);
            JSONArray dataArray = entries.getJSONArray("data");
            List<NccBasicDept> nccBasicDepts = dataArray.toList(NccBasicDept.class);
            for (NccBasicDept nccBasicDept : nccBasicDepts) {
                NccBasicDept basicDept = nccBasicDeptMapper.queryNccBasicDept(nccBasicDept.getCode());
                if (basicDept != null) {
                    nccBasicDeptMapper.updateNccBasicDept(nccBasicDept);
                } else {
                    nccBasicDeptMapper.insertNccBasicDept(nccBasicDept);
                }
            }
        }
    }

    /**
     * 发送请求
     *
     * @param i
     * @param startTimeStr
     * @param endTimeStr
     * @param platformCode
     * @return
     */
    public HttpResponse methodExecute(String startTimeStr, String endTimeStr, Integer i, Integer platformCode) {
        String url = "http://192.168.132.202:8010/WMS/GetBaseData";
        HashMap<String, Object> hashMap = new HashMap<>();

        // 添加post请求参数
        hashMap.put("startTime", startTimeStr);
        hashMap.put("endTime", endTimeStr);
        hashMap.put("nccBaseDataEnum", i);
        hashMap.put("platformCode", platformCode);
        String jsonString = JSONUtil.toJsonStr(hashMap);

        // 添加请求头并执行请求
        HttpResponse response = HttpUtil.createPost(basicInfoUrl)
                .header("sourceSystem", sourceSystem)
                .header("targetSystem", targetSystem)
                .header("serviceName", serviceName)
                .header("contentType",contentType)
                .body(jsonString, "application/json")
                .execute();
        return response;
    }
}
